package com.example.newdemo.utils.timeUtils;


import androidx.annotation.NonNull;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeAndDateUtils {


    /**
     * string类型的date日期转换为date
     *
     * @param dateString string类型的date日期
     * @param dateFormat 日期格式
     * @return
     */
    public static Date dateStringToDate(String dateString, TimeEnum dateFormat) {

        Date date = null;
        try {
            DateFormat df = new SimpleDateFormat(dateFormat.getFormat());
            date = df.parse(dateString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;

    }


    public static String dateToDateString(@NonNull Date date, @NonNull TimeEnum dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat.getFormat());
        return format.format(date);
    }


    /**
     * 获取月份，0表示1月份
     * @param calendar
     * @return
     */
    public static int getMonth(Calendar calendar) {
        return calendar.get(Calendar.MONTH) + 1;
    }

    public static int getYear(Calendar calendar) {
        return calendar.get(Calendar.YEAR);
    }

    public static int getDay(Calendar calendar) {
        return calendar.get(Calendar.DATE);
    }

    public static int getWeek(Calendar calendar) {
        return calendar.get(Calendar.DAY_OF_WEEK);
    }


    public static int getHour(Calendar calendar) {


        return calendar.get(Calendar.HOUR_OF_DAY);//获取当前小时
    }

    public static int getMinute(Calendar calendar) {
        return calendar.get(Calendar.MINUTE);//获取当前分钟
    }


    public static int getSecond(Calendar calendar) {
        return calendar.get(Calendar.SECOND);//获取当前秒
    }


    public static int getCurrentMonthMin(Calendar calendar) {
        return calendar.getActualMinimum(Calendar.DAY_OF_MONTH);//获取本月最小天数
    }


    public static int getCurrentMonthMax(Calendar calendar) {
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);//获取本月最大天数
    }


    public static int getMonth(Date date) {
        return getMonth(DateAndCalendarUtils.dateToCalendar(date));
    }


}
